#include"points.h"
TopLeftPoint::TopLeftPoint(qreal x,qreal y,QSelectEditTool* t):QGraphicsEllipseItem(-5,-5,10,10),tool(t)
{
	this->setPos(x,y);
	this->setBrush(QBrush(QColor(0,0,0)));
	this->setFlag(QGraphicsEllipseItem::ItemIsMovable);
	this->setFlag(QGraphicsItem::ItemIsSelectable);
}

void TopLeftPoint::mousePressEvent(QGraphicsSceneMouseEvent* event)
	{
		QGraphicsEllipseItem::mousePressEvent(event);
	}
void TopLeftPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
	{
		QPointF p=this->pos();
		//tool->blp=tool->currentItem->mapFromScene(p);
		QPointF mouse=this->pos();
		mouse=this->tool->currentItem->mapFromScene(mouse);
		QPointF brp=this->tool->brp;;
		double nx=brp.x()-mouse.x();
		double ny=brp.y()-mouse.y();
		if(nx<0.1)
			nx=abs(nx)/nx*1;
		if(ny<0.1)
			ny=abs(ny)/ny;
		double newxscale=(nx)/this->tool->oriwidth;
		double newyscale=(ny)/this->tool->oriheight;
		//QTrasform trn=((customRectItem*)(this->controledItem))
		QTransform tr0=QTransform(1,0,0,1,mouse.x(),mouse.y());
		QTransform tr=QTransform(newxscale,0,0,newyscale,0,0);
		this->tool->currentItem->setTransform(tr0,true);
		this->tool->currentItem->setTransform(tr,true);
		this->tool->Scale=tr*this->tool->Scale;
		//((customRectItem*)(this->controledItem))->translationTr=tr0*((customRectItem*)(this->controledItem))->translationTr;
		this->tool->updatePoints();
		QGraphicsEllipseItem::mouseMoveEvent(event);
}
int	TopLeftPoint::type() const
{
	return Type;
}
/******************************************************************************************************************************************/
TopRightPoint::TopRightPoint(qreal x,qreal y,QSelectEditTool* t):QGraphicsEllipseItem(-5,-5,10,10),tool(t)
{
	this->setPos(x,y);
	this->setBrush(QBrush(QColor(0,0,0)));
	this->setFlag(QGraphicsItem::ItemIsMovable,true);
	this->setFlag(QGraphicsItem::ItemIsSelectable,true);
}
void TopRightPoint::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
	{
		//QPointF p=this->pos();
		////tool->trp=tool->currentItem->mapFromScene(p);
		//QPointF mouse=this->pos();
		////tool->brp=mouse;
		//mouse=this->tool->currentItem->mapFromScene(mouse);
		//QPointF bp=this->tool->blp;
  //      double nx=mouse.x();
		//double ny=bp.y()-mouse.y();
		//if(nx<0.1)
		//	nx=abs(nx)/nx*1;
		//if(ny<0.1)
		//	ny=abs(ny)/ny;
		//double newxscale=(nx)/this->tool->oriwidth;
		//double newyscale=(ny)*1.0/tool->oriheight;
		//QTransform tr0=QTransform(1,0,0,1,0,mouse.y());
		//QTransform tr=QTransform(newxscale,0,0,newyscale,0,0);
		//this->tool->currentItem->setTransform(tr0,true);
		//this->tool->currentItem->setTransform(tr,true);
		//QTransform tr56=this->tool->currentItem->transform();
		////this->parentItem->setScale(newxscale);
		//this->tool->Scale=tr*this->tool->Scale;
		//this->tool->updatePoints();
		QGraphicsEllipseItem::mouseDoubleClickEvent(event);
	}
void TopRightPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
	{
		QPointF p=this->pos();
		//tool->blp=tool->currentItem->mapFromScene(p);
		QPointF mouse=this->pos();
		//tool->brp=mouse;
		mouse=this->tool->currentItem->mapFromScene(mouse);
		QPointF bp=this->tool->blp;
        double nx=mouse.x();
		double ny=bp.y()-mouse.y();
		if(nx<0.1)
			nx=abs(nx)/nx*1;
		if(ny<0.1)
			ny=abs(ny)/ny;
		double newxscale=(nx)/this->tool->oriwidth;
		double newyscale=(ny)*1.0/tool->oriheight;
		QTransform tr0=QTransform(1,0,0,1,0,mouse.y());
		QTransform tr=QTransform(newxscale,0,0,newyscale,0,0);
		this->tool->currentItem->setTransform(tr0,true);
		this->tool->currentItem->setTransform(tr,true);
		//this->parentItem->setScale(newxscale);
		this->tool->Scale=tr*this->tool->Scale;
		this->tool->updatePoints();
		QGraphicsEllipseItem::mouseMoveEvent(event);
	}
int	TopRightPoint::type() const
{
	return Type;
}
/******************************************************************************************************************************************/
BottomLeftPoint::BottomLeftPoint(qreal x,qreal y,QSelectEditTool* t):QGraphicsEllipseItem(-5,-5,10,10),tool(t)
{
	this->setPos(x,y);
	this->setBrush(QBrush(QColor(0,0,0)));
	this->setFlag(QGraphicsEllipseItem::ItemIsMovable);
	this->setFlag(QGraphicsItem::ItemIsSelectable);
}
void BottomLeftPoint::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
	{
		QGraphicsEllipseItem::mouseDoubleClickEvent(event);
	}
void BottomLeftPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
	{
		QPointF p=this->pos();
		QPointF mouse=this->pos();
		
		mouse=tool->currentItem->mapFromScene(mouse);
		//tool->blp=mouse;
		QPointF bp=this->tool->trp;
	    double nx=bp.x()-mouse.x();
		double ny=mouse.y();
		if(nx<0.1)
			nx=abs(nx)/nx*1;
		if(ny<0.1)
			ny=abs(ny)/ny;
		double newxscale=(nx)/tool->oriwidth;
		double newyscale=(ny)*(1.0)/tool->oriheight;
		QTransform tr0=QTransform(1,0,0,1,mouse.x(),0);
		QTransform tr=QTransform(newxscale,0,0,newyscale,0,0);
		tool->currentItem->setTransform(tr0,true);
	     tool->currentItem->setTransform(tr,true);
		 tool->Scale=tr*tool->Scale;
		tool->updatePoints();
		QGraphicsEllipseItem::mouseMoveEvent(event);
	}
int	BottomLeftPoint::type() const
{
	return Type;
}
/******************************************************************************************************************************************/
BottomRightPoint::BottomRightPoint(qreal x,qreal y,QSelectEditTool* t):QGraphicsEllipseItem(-5,-5,10,10),tool(t)
{
	this->setPos(x,y);
	this->setBrush(QBrush(QColor(0,0,0)));
	this->setFlag(QGraphicsEllipseItem::ItemIsMovable);
	this->setFlag(QGraphicsItem::ItemIsSelectable);
}


void BottomRightPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
	{
		QPointF p=this->pos();
		QPointF mouse=this->pos();
		mouse=tool->currentItem->mapFromScene(mouse);
		QPointF bp=tool->tlp;;
		double nx=mouse.x()-bp.x();
		double ny=mouse.y()-bp.y();
		if(nx<0.1)
			nx=abs(nx)/nx*1;
		if(ny<0.1)
			ny=abs(ny)/ny;
		double newxscale=nx/tool->oriwidth;
		double newyscale=ny/tool->oriheight;;
		QTransform tr=QTransform(newxscale,0,0,newyscale,0,0);
		tool->currentItem->setTransform(tr,true);
		tool->Scale=tr*tool->Scale;
		tool->updatePoints();
		QGraphicsEllipseItem::mouseMoveEvent(event);
	}
int	BottomRightPoint::type() const
{
	return Type;
}
/****************************************************************************************************************************************************************/
RotationCenterPoint::RotationCenterPoint(qreal x,qreal y,QSelectEditTool* t):QGraphicsEllipseItem(-5,-5,10,10),tool(t)
{
	this->setPos(x,y);
	this->setBrush(QBrush(QColor(255,0,0)));
	this->setFlag(QGraphicsEllipseItem::ItemIsMovable);
	this->setFlag(QGraphicsItem::ItemIsSelectable);
}

void RotationCenterPoint::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
	{
		QTransform re=tool->currentItem->transform();
		QGraphicsEllipseItem::mouseDoubleClickEvent(event);
	}
void RotationCenterPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
	{
		tool->rcp=tool->currentItem->mapFromScene(this->pos());
		tool->AxisTrans=QTransform(1,0,0,1,-tool->rcp.x(),-tool->rcp.y());
		tool->Translation=QTransform(1,0,0,1,this->pos().x(),this->pos().y());
		QGraphicsEllipseItem::mouseMoveEvent(event);
	}
int	RotationCenterPoint::type() const
{
	return Type;
}
/**********************************************************************************************************************************************************/
RotationControlPoint::RotationControlPoint(qreal x,qreal y,QSelectEditTool* t):QGraphicsEllipseItem(-5,-5,10,10),tool(t)
{
	this->setPos(x,y);
	this->setBrush(QBrush(QColor(255,0,0)));
	this->setFlag(QGraphicsEllipseItem::ItemIsMovable);
	this->setFlag(QGraphicsItem::ItemIsSelectable);
}
void RotationControlPoint::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
	QGraphicsEllipseItem::mouseDoubleClickEvent(event);
}
void RotationControlPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
	{
	    QPointF final=this->pos();
		this->tool->rconp=this->tool->currentItem->mapFromScene(final);
		QPointF c=tool->currentItem->mapToScene(tool->rcp);
		qreal r;
		qreal thetatg=(final.x()-c.x())/(c.y()-final.y());
		qreal theta=atan(thetatg);
		if(theta>0&&(final.x()-c.x()>0))
			;
		else if(theta>0&&(final.x()-c.x()<0))
			theta+=acos(-1.0);
		else if(theta<0&&(final.x()-c.x()>0))
			theta+=acos(-1.0);
		else if(theta<0&&(final.x()-c.x())>0)
			theta+=2*acos(-1.0);

		else
			;
		if(abs(c.y()-final.y())<1e-3)
			theta=1.5*(acos(-1.0));
		r=theta;
		qreal cosc=cos(r);
		qreal sinc=sin(r);
		QTransform tr1=QTransform(cosc,sinc,-sinc,cosc,0,0);
		QTransform tr0=tool->AxisTrans;
		this->tool->Rotation=tr1;
		tool->currentItem->resetTransform();
		tool->currentItem->setPos(0,0);
		tool->currentItem->setTransform(tool->Translation);
		tool->currentItem->setTransform(tool->Skew,true);
		tool->currentItem->setTransform(tool->Rotation,true);
		tool->currentItem->setTransform(tool->Scale,true);
		tool->currentItem->setTransform(tr0,true);
		tool->updatePoints();
		QGraphicsEllipseItem::mouseMoveEvent(event);
	}
int	RotationControlPoint::type() const
{
	return Type;
}
/*******************************************************************************************************************************************/
TopCenterPoint::TopCenterPoint(qreal x,qreal y,QSelectEditTool* t):QGraphicsEllipseItem(-5,-5,10,10),tool(t)
{
	this->setPos(x,y);
	this->setBrush(QBrush(QColor(0,255,0)));
	this->setFlag(QGraphicsEllipseItem::ItemIsMovable);
	this->setFlag(QGraphicsItem::ItemIsSelectable);
}
void TopCenterPoint::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
	{
		QGraphicsEllipseItem::mouseDoubleClickEvent(event);
	}
void TopCenterPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
	{
	  //((customRectItem*)(this->controledItem))->rcp=((customRectItem*)(this->controledItem))->mapFromScene(this->pos());
		QPointF mouse=this->pos();
		//tool->tcp=tool->currentItem->mapFromScene(mouse);
		mouse=tool->currentItem->mapFromScene(mouse);
		qreal x=mouse.x()-tool->tcp.x();
		qreal y=tool->oriheight;
		qreal tg=x/y;
		if(abs(tg)>4)
			tg=-4*abs(tg)/tg;
		QTransform tr=QTransform(1,0,-tg,1,0,0);
		QTransform tr0=QTransform(1,0,0,1,x,0);
		tool->Skew=tr*tool->Skew;
		tool->currentItem->setTransform(tr0,true);
		tool->currentItem->setTransform(tr,true);
		tool->updatePoints();
		QGraphicsEllipseItem::mouseMoveEvent(event);
		//this->setPos(((customRectItem*)(this->controledItem))->mapToScene(((customRectItem*)(this->controledItem))->tcp));
		//this->update();
	}
int	TopCenterPoint::type() const
{
	return Type;
}
/******************************************************************************************************************************/
RightCenterPoint::RightCenterPoint(qreal x,qreal y,QSelectEditTool* t):QGraphicsEllipseItem(-5,-5,10,10),tool(t)
{
	this->setPos(x,y);
	this->setBrush(QBrush(QColor(0,255,0)));
	this->setFlag(QGraphicsEllipseItem::ItemIsMovable);
	this->setFlag(QGraphicsItem::ItemIsSelectable);
}

void RightCenterPoint::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
	{
		
		QGraphicsEllipseItem::mouseDoubleClickEvent(event);
	}
void RightCenterPoint::mouseMoveEvent(QGraphicsSceneMouseEvent* event)
	{
	  //((customRectItem*)(this->controledItem))->rcp=((customRectItem*)(this->controledItem))->mapFromScene(this->pos());
		QPointF mouse=this->pos();
		//tool->rcenterp=tool->currentItem->mapFromScene(mouse);
		mouse=tool->currentItem->mapFromScene(mouse);
		qreal x=mouse.y()-tool->rcenterp.y();
		qreal y=tool->oriwidth;
		qreal tg=x/y;
		if(abs(tg)>999)
			tg=999*abs(tg)/tg;
		QTransform tr=QTransform(1,tg,0,1,0,0);
		//QTransform tr0=QTransform(1,0,0,1,x,0);
		//((customRectItem*)(this->controledItem))->setTransform(tr0,true);
		tool->currentItem->setTransform(tr,true);
		tool->Skew=tr*tool->Skew;
		tool->updatePoints();
		QGraphicsEllipseItem::mouseMoveEvent(event);
	}
int	RightCenterPoint::type() const
{
	return Type;
}
/******************************************************************************************************************************/